-- Drawing callback

local repl = require("love-repl") 

function love.draw()

-- console
	if repl.toggled() then
	    repl.draw()
	    return
	 end


-- basic tiles
	for i = 1, #localTerrain, 1 do
		for j = 1,#localTerrain[i],1 do

			-- tile is visible to a visible enemy!
			local enemyVisible = false
    		for i,v in pairs(localTerrain[i][j].MobFOV) do
  				if (localTerrain[v.x][v.y].FOV == true ) then
  					enemyVisible = true
  					break
  				end
			end

			if (localTerrain[i][j].FOV == true) then

				if (enemyVisible == true) then  -- this is fine
					love.graphics.setColor(255,155,155,255)
				else
				    love.graphics.setColor(255,255,255,255)
				end

				-- Firing range FOV, has priority over enemy range
				local firingRange = false
				if (controlMode == "targetmode" ) then
					if (playerItems[usingItem]~= nil and playerItems[usingItem].range ~= nil ) then
						if (( i == characterLoc[1] and j == characterLoc[2]) == false)  then
							if (ComputeFOV(characterLoc, playerHeading.value, {i,j}, playerItems[usingItem].range, false)== true) then
								if (isObjectBlocking(i,j) == false) then
									love.graphics.setColor(66,180,230,255)
									firingRange = true
								end
							end
						end
					end
				end

			else
				if (enemyVisible == true) then 
					love.graphics.setColor(155,55,55,255)
				else
				    love.graphics.setColor(155,155,155,255)
				end
			end

			-- draw tile
			love.graphics.drawq(snowTileMap,terrainTiles[localTerrain[i][j].tileID],i*tileSize,j*tileSize)

			if (localTerrain[i][j].hasObject == true) then
				love.graphics.drawq(snowTileMap,objectSprites[localTerrain[i][j].objectID],i*tileSize,j*tileSize)
			end

			if (localTerrain[i][j].FOV == true and isItemOnGround(i,j) == true) then
				love.graphics.drawq(snowTileMap,itemQuad,i*tileSize,j*tileSize)
			end
		end
	end

	-- draw mobs
	love.graphics.setColor(255,255,255,255)
	local mobList = activeMobs
    while mobList do

    	local mx = mobList.value.x
    	local my = mobList.value.y

    	if (localTerrain[mx] and localTerrain[mx][my] and localTerrain[mx][my].FOV == true) then

    		local quad;
    		if (mobList.value.alive == true )then
       			quad = mobSprites[mobList.value.mobID].alive
       		else
       			quad = mobSprites[mobList.value.mobID].dead
       		end

       		love.graphics.drawq(snowTileMap,quad,tileSize*mobList.value.x,tileSize*mobList.value.y)

       		-- draw headings
       		if (mobList.value.alive == true) then

       			if (mobList.value.isMoving == true) then
       				love.graphics.setColor(255,0,0,255)
       			else
       				love.graphics.setColor(0,0,255,255)
       			end

       			-- draw current heading
       			love.graphics.drawq(snowTileMap,mobList.value.heading.value.quad,tileSize*mobList.value.x,tileSize*mobList.value.y)


				love.graphics.setColor(255,255,255,255)

				-- turning heading, don't draw if the mob is alerted (it's usually wrong when running away
				-- as the astar keeps recalculating the best path)
       			if (mobList.value.isTurning and mobList.value.alerted == false ) then
       				love.graphics.setColor(0,255,0,255)
       				love.graphics.drawq(snowTileMap,mobList.value.targetHeading.value.quad,tileSize*mobList.value.x,tileSize*mobList.value.y)
					love.graphics.setColor(255,255,255,255)
       			end

       			-- alert notifier
       			if (mobList.value.alerted == true) then
       				love.graphics.drawq(snowTileMap,alertQuad,tileSize*mobList.value.x,tileSize*mobList.value.y)
       			end

       		end

       	end

      	mobList = mobList.next
    end

    -- draw character
    love.graphics.setColor(255,255,255,255)
	love.graphics.drawq(snowTileMap,characterQuad,tileSize*characterLoc[1],tileSize*characterLoc[2])

	-- draw heading
	love.graphics.setColor(0,0,255,255)
	love.graphics.drawq(snowTileMap,playerHeading.value.quad,tileSize*characterLoc[1],tileSize*characterLoc[2])
    love.graphics.setColor(255,255,255,255)


		-- go through and draw treetops
	for i = 1, #localTerrain, 1 do
		for j = 1,#localTerrain[i],1 do
			if (localTerrain[i][j].hasObject == true and localTerrain[i][j].objectID == "TreeBase") then
				if (localTerrain[i][j-1] ~= nil) then
					if (localTerrain[i][j-1].FOV == true or localTerrain[i][j].FOV == true) then
						love.graphics.setColor(255,255,255,255)
					else
						love.graphics.setColor(155,155,155,255)
					end

					love.graphics.drawq(snowTileMap,objectSprites["TreeTop"],i*tileSize,(j-1)*tileSize)

				end

			end
		end
	end

	love.graphics.setColor(255,255,255,255)

	-- if targetmode, draw crosshair
	if (controlMode == "targetmode") then
		love.graphics.drawq(snowTileMap,crossHairQuad, targetLoc[1]*tileSize, targetLoc[2]*tileSize)
	end


	-- title
	love.graphics.print("ColdRL - 7DRL 2013", 340,10)

	local hbase = 530

	love.graphics.quad("line", hbase, 32, hbase+250, 32+0, hbase+250, 32+220, hbase+0, 32+220)

	if (characterHeat > 80 ) then love.graphics.setColor(0,255,0,255)
	elseif (characterHeat > 50 ) then love.graphics.setColor(255,255,0,255)
	elseif (characterHeat > 35 ) then love.graphics.setColor(255,155,0,255) 
	else love.graphics.setColor(255,0,0,255) end

	love.graphics.print("Core Temperature: " .. 33.0 + 4*characterHeat/100 .. "° ("..characterHeat.."%)" , hbase+5,37)

	if (characterHealth > 80 ) then love.graphics.setColor(0,255,0,255)
	elseif (characterHealth > 50 ) then love.graphics.setColor(255,255,0,255)
	elseif (characterHealth > 35 ) then love.graphics.setColor(255,155,0,255) 
	else love.graphics.setColor(255,0,0,255) end

	love.graphics.print("Physical Health:  " .. math.ceil(characterHealth) .. "%" , hbase+5,57)

	love.graphics.setColor(255,255,255,255)

	love.graphics.print("Armour Level ".. getArmourLevel() ..": ", hbase + 5, 97)
	love.graphics.print("You are " .. armourString[getArmourLevel()].."." , hbase+5,117)

	love.graphics.print("Distance to escape:  " .. howFarLeft() .. "m" , hbase+5,157)


	local ibase = 260

	love.graphics.quad("line", hbase, ibase, hbase+250, ibase+0, hbase+250, ibase+250, hbase+0, ibase+250)

	hbase = hbase + 5
	ibase = ibase + 5

	love.graphics.print("Weapons:" , hbase,ibase)
	love.graphics.print("_____________________________________",hbase,ibase)

	for i = 1,2,1 do

		if (controlMode == "targetmode" and usingItem == i) then
			love.graphics.setColor(255,0,0,255)
		else
			love.graphics.setColor(255,255,255,255)
		end

		love.graphics.print(itemDesc(i,playerItems[i]), hbase,ibase+20*i)
	end

	love.graphics.setColor(255,255,255,255)

	love.graphics.print("Items:" , hbase,ibase+80)
	love.graphics.print("_____________________________________",hbase,ibase+80)
	for i = 3,9,1 do

		if (controlMode == "targetmode" and usingItem == i) then
			love.graphics.setColor(255,0,0,255)
		elseif (playerItems[i].active ~= nil and playerItems[i].active == true) then
			love.graphics.setColor(0,255,0,255)
		else
			love.graphics.setColor(255,255,255,255)
		end

		love.graphics.print(itemDesc(i,playerItems[i]), hbase,ibase+80 + (i-2)*20)
	end

	love.graphics.setColor(255,255,255,255)

	-- Draw console output
	drawConsole( 32, 520 )

	-- draw in game panel
	drawPanel()

end


function drawPanel()


	if (getLevel() >= 4) then
		drawWinning()
		return
	end

	-- pick up object panel
	if (controlMode == "pickup") then
		love.graphics.setColor(0,0,0,200)

		local xbase = 150
		local ybase = 100

		local xsize = 300
		local ysize = 300

		love.graphics.quad("fill", xbase, ybase, xbase+xsize, ybase+0, xbase+xsize, ybase+ysize, xbase+0, ybase+ysize)

		love.graphics.setColor(255,255,255,255)
		love.graphics.quad("line", xbase, ybase, xbase+xsize, ybase+0, xbase+xsize, ybase+ysize, xbase+0, ybase+ysize)

		love.graphics.print("Get which item?", xbase+100,ybase+5)

		index = 1
		for i=1,10,1 do
			if (localTerrain[characterLoc[1]][characterLoc[2]].items[i].itemtype ~= "empty")  then
				if (targetItem ~= nil and targetItem == i) then love.graphics.setColor(255,255,0,255) 
					else love.graphics.setColor(255,255,255,255) end
				love.graphics.print(itemDesc(letterMap[i],localTerrain[characterLoc[1]][characterLoc[2]].items[i]),xbase + 20, ybase + 15+20*(index))
				index = index+1
			end
		end


		if (targetItem ~= nil) then
			love.graphics.setColor(0,255,0,255)

			local targetType = localTerrain[characterLoc[1]][characterLoc[2]].items[targetItem].itemtype
			if ( targetType == "melee" or targetType == "gun") then
				love.graphics.print("Select a weapon slot: 1,2", xbase + 75, ybase + ysize - 40)
			else
				love.graphics.print("Select an item slot: 3-9", xbase + 80, ybase + ysize - 40)
			end

			love.graphics.setColor(255,255,255,255)
		end
		love.graphics.print("Press Escape to Cancel", xbase + 80, ybase + ysize - 20)
	end


	if (controlMode == "dropmode") then
		love.graphics.setColor(0,0,0,200)

		local xbase = 180
		local ybase = 250

		local xsize = 200
		local ysize = 55

		love.graphics.quad("fill", xbase, ybase, xbase+xsize, ybase+0, xbase+xsize, ybase+ysize, xbase+0, ybase+ysize)

		love.graphics.setColor(255,255,255,255)
		love.graphics.quad("line", xbase, ybase, xbase+xsize, ybase+0, xbase+xsize, ybase+ysize, xbase+0, ybase+ysize)
		love.graphics.setColor(255,255,0,255)

		love.graphics.print("Drop which item? (1-9)", xbase+30,ybase+7)
		love.graphics.setColor(255,255,255,255)

		love.graphics.print("Press Escape to Cancel", xbase + 25, ybase +30)


	end

	if (controlMode == "deadmode") then

		love.graphics.setColor(0,0,0,200)

		local xbase = 180
		local ybase = 250

		local xsize = 200
		local ysize = 55

		love.graphics.quad("fill", xbase, ybase, xbase+xsize, ybase+0, xbase+xsize, ybase+ysize, xbase+0, ybase+ysize)

		love.graphics.setColor(255,255,255,255)
		love.graphics.quad("line", xbase, ybase, xbase+xsize, ybase+0, xbase+xsize, ybase+ysize, xbase+0, ybase+ysize)
		love.graphics.setColor(255,255,0,255)

		love.graphics.print("YOU ARE DEAD", xbase+50,ybase+7)
		love.graphics.setColor(255,255,255,255)

		love.graphics.print("Press r to restart", xbase + 45, ybase +30)

	end

	if (controlMode == "consolemode") then

		drawBigConsole()

	end


	return
end


function drawWinning()
	love.graphics.setColor(0,0,0,200)

		local xbase = 300
		local ybase = 100

		local xsize = 200
		local ysize = 200

		love.graphics.quad("fill", xbase, ybase, xbase+xsize, ybase+0, xbase+xsize, ybase+ysize, xbase+0, ybase+ysize)

		love.graphics.setColor(255,255,255,255)
		love.graphics.quad("line", xbase, ybase, xbase+xsize, ybase+0, xbase+xsize, ybase+ysize, xbase+0, ybase+ysize)
		love.graphics.setColor(255,255,0,255)

		ybase = ybase + 25

		love.graphics.print("CONGRATULATIONS", xbase+40,ybase+7)
		love.graphics.setColor(255,255,255,255)

		love.graphics.print("You escaped the tundra!", xbase + 20, ybase +30)
		love.graphics.setColor(0,255,0,255)

		love.graphics.print("Kills: " .. characterKills, xbase + 50, ybase + 60)
		love.graphics.print("Turns: " .. characterTurns, xbase + 50, ybase + 80)
		love.graphics.setColor(255,255,255,255)
		love.graphics.print("Press r to restart", xbase + 40, ybase + 120)

end



















